<?php
echo <<<EOF
<html>
<head><title>EhModuleWeb Testes :: Teste 01 para EhDB.class.php</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/></hread>
<pre>
/**
 * test-01.EhDB.class.php, Testes para classe EhDB
 *
 * Este arquivo faz parte do Framework EhModuleWeb
 *
 * @version 0.1
 * @author Erlimar Silva Campos <erlimar.campos@ehsistemas.com.br>
 * @date  Junho 21, 2011 23:08 PM
 *
 *
 * Copyright (C) 2011 EH SISTEMAS DE INFORMÁTICA ME
 * 
 */


EOF;

require_once("../lib/EhModuleWeb.php");

echo "-------- BEGIN TESTS -------------------------------------------------\n";

//Definindo uma constante para demonstrar o uso de constantes nas strings DBSN
define ("VENDORNAME","Eh Sistemas");

function getArrayStr($array)
{
    $p = "";
    foreach ( $array as $k => $v)
    {
        if (strlen ($p) < 1)
            $p .= "([" . $k . (constant ($k) ? " {" . constant ($k) . "}" : '' ) . "] => " . ($v ? '"' . $v . '"' : 'null');
        else
            $p .= ", [" . $k . (constant ($k) ? " {" . constant ($k) . "}" : '' ) . "] => " . ($v ? '"' . $v . '"' : 'null');
    }
    
    if (strlen ($p) > 0 )
        $p .= ")";
    
    return $p;
}

echo <<<EOF
Teste 01, Usando DBSN completo sem parâmetros.


EOF;

try
{
    $db1 = new EhDB("driver://usuario:senha@servidor:porta/bancodedados/?copyright=VENDORNAME&data=DATE_W3C");
}
catch (Exception $e)
{
    $error = $e->getMessage();
}
if ($db1)
{
    echo "DBSN      = " . htmlentities($db1->getDBSN()) . "\n";
    echo "Engine    = " . htmlentities($db1->getEngine()) . "\n";
    echo "Hostname  = " . htmlentities($db1->getHostname()) . "\n";
    echo "Port      = " . htmlentities($db1->getPort()) . "\n";
    echo "Database  = " . htmlentities($db1->getDatabase()) . "\n";
    echo "Username  = " . htmlentities($db1->getUsername()) . "\n";
    echo "Password  = " . htmlentities($db1->getPassword()) . "\n";
    echo "Params    = " . getArrayStr($db1->getParams()) . "\n";
    echo "PDOParams = " . getArrayStr($db1->GetPDOParams()) . "\n";
}else
    echo "Erro criando \$db1: " . $error . "\n";

echo "<hr />";

echo <<<EOF
Teste 02, Usando DBSN completo com parâmetros sem valores.


EOF;

try
{
    $db2 = new EhDB("driver://usuario:senha@servidor:0123456789/bancodedados/?parametro1&parametro2");
}
catch (Exception $e)
{
    $error = $e->getMessage();
}
if ($db2)
{
    echo "DBSN      = " . htmlentities($db2->getDBSN()) . "\n";
    echo "Engine    = " . htmlentities($db2->getEngine()) . "\n";
    echo "Hostname  = " . htmlentities($db2->getHostname()) . "\n";
    echo "Port      = " . htmlentities($db2->getPort()) . "\n";
    echo "Database  = " . htmlentities($db2->getDatabase()) . "\n";
    echo "Username  = " . htmlentities($db2->getUsername()) . "\n";
    echo "Password  = " . htmlentities($db2->getPassword()) . "\n";
    echo "Params    = " . getArrayStr($db2->getParams()) . "\n";
    echo "PDOParams = " . getArrayStr($db2->GetPDOParams()) . "\n";
}else
    echo "Erro criando \$db2: " . $error . "\n";
    
echo "<hr />";

echo <<<EOF
Teste 03, Usando DBSN parcial com parâmetros e valores.


EOF;

try
{
    $db3 = new EhDB("driver://usuario@servidor/bancodedados?param1=valor do parametro 1&param2=13.5&param3=entao&param4=DB_SIZE_DEFAULT");
}
catch (Exception $e)
{
    $error = $e->getMessage();
}
if ($db3)
{
    echo "DBSN      = " . htmlentities($db3->getDBSN()) . "\n";
    echo "Engine    = " . htmlentities($db3->getEngine()) . "\n";
    echo "Hostname  = " . htmlentities($db3->getHostname()) . "\n";
    echo "Port      = " . htmlentities($db3->getPort()) . "\n";
    echo "Database  = " . htmlentities($db3->getDatabase()) . "\n";
    echo "Username  = " . htmlentities($db3->getUsername()) . "\n";
    echo "Password  = " . htmlentities($db3->getPassword()) . "\n";
    echo "Params    = " . getArrayStr($db3->getParams()) . "\n";
    echo "PDOParams = " . getArrayStr($db3->GetPDOParams()) . "\n";
}else
    echo "Erro criando \$db3: " . $error . "\n";

echo "<hr />";

echo <<<EOF
Teste 04, Usando DBSN parcial com parâmetros e valores; e parâmetros PDO.


EOF;

try
{
    $db4 = new EhDB("driver://usuario@servidor/bancodedados?param1=valor&param2=13.5&PDO::ATTR_CASE=PDO::CASE_LOWER&PDO::ATTR_ERRMODE=PDO::ERRMODE_EXCEPTION&PDO::ATTR_CASE");
}
catch (Exception $e)
{
    $error = $e->getMessage();
}
if ($db4)
{
    echo "DBSN      = " . htmlentities($db4->getDBSN()) . "\n";
    echo "Engine    = " . htmlentities($db4->getEngine()) . "\n";
    echo "Hostname  = " . htmlentities($db4->getHostname()) . "\n";
    echo "Port      = " . htmlentities($db4->getPort()) . "\n";
    echo "Database  = " . htmlentities($db4->getDatabase()) . "\n";
    echo "Username  = " . htmlentities($db4->getUsername()) . "\n";
    echo "Password  = " . htmlentities($db4->getPassword()) . "\n";
    echo "Params    = " . getArrayStr($db4->getParams()) . "\n";
    echo "PDOParams = " . getArrayStr($db4->GetPDOParams()) . "\n";
}else
    echo "Erro criando \$db4: " . $error . "\n";
    
echo "<hr />";

echo <<<EOF
Teste 05, Um teste mais próximo do real.


EOF;

try
{
    $db5 = new EhDB("mysql://root@localhost/mysql");
}
catch (Exception $e)
{
    $error = $e->getMessage();
}
if ($db5)
{
    echo "DBSN      = " . htmlentities($db5->getDBSN()) . "\n";
    echo "Engine    = " . htmlentities($db5->getEngine()) . "\n";
    echo "Hostname  = " . htmlentities($db5->getHostname()) . "\n";
    echo "Port      = " . htmlentities($db5->getPort()) . "\n";
    echo "Database  = " . htmlentities($db5->getDatabase()) . "\n";
    echo "Username  = " . htmlentities($db5->getUsername()) . "\n";
    echo "Password  = " . htmlentities($db5->getPassword()) . "\n";
    echo "Params    = " . getArrayStr($db5->getParams()) . "\n";
    echo "PDOParams = " . getArrayStr($db5->GetPDOParams()) . "\n";
}else
    echo "Erro criando \$db5: " . $error . "\n";

echo "-------- END TESTS ---------------------------------------------------\n";
echo "</pre></html>";
?>
